﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2019-01-09 14:14:41
// 负责人：liucx
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using System.Configuration;

namespace OF.Component.DAL
{
    #region 模版城市变更历史记录 数据访问层
    /// <summary>
    ///模版城市变更历史记录 数据访问层
    /// </summary>	
    internal partial class ExpressFeeTplCityHistory : IExpressFeeTplCityHistory
    {

        //连接字符串
        public virtual string ConnectionStringKey
        {
            get
            {
                return ConstantUtility.CGTConnectionString;
            }
        }
        #region GetMaxID



        public int GetMaxID()
        {
            int nReturn = 0;
            string szSql = @"SELECT ISNULL(MAX(HistoryID),0)+1
                               FROM [ExpressFeeTplCityHistory]";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(szSql);
            try
            {
                object o = db.ExecuteScalar(dbc);
                if (o != null)
                {
                    nReturn = Convert.ToInt32(o);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nReturn;
        }



        #endregion

        #region Basic CRUD functions
        private ExpressFeeTplCityHistoryInfo GetByReader(IDataReader dr)
        {
            ExpressFeeTplCityHistoryInfo info = new ExpressFeeTplCityHistoryInfo();
            object obj;
            obj = dr["HistoryID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.HistoryID = (long)obj;
            }
            obj = dr["ExpressFeeTplCity"];
            if (obj != null && obj != DBNull.Value)
            {
                info.ExpressFeeTplCity = Convert.ToInt32(obj);
            }
            obj = dr["ExpressFeeTplID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.ExpressFeeTplID = Convert.ToInt32(obj);
            }
            obj = dr["CityID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.CityID = Convert.ToInt32(obj);
            }
            obj = dr["UpdateDate"];
            if (obj != null && obj != DBNull.Value)
            {
                info.UpdateDate = Convert.ToDateTime(obj);
            }
            obj = dr["UpdatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.UpdatedUserID = Convert.ToInt32(obj);
            }
            return info;
        }
        public long Insert(ExpressFeeTplCityHistoryInfo info)
        {


            string strSql = @"INSERT INTO ExpressFeeTplCityHistory			
			(			
			[HistoryID],
			[ExpressFeeTplID],
			[CityID],
			[UpdateDate],
			[UpdatedUserID]
			)
			 VALUES
			(			
			@HistoryID,
			@ExpressFeeTplID,
			@CityID,
			@UpdateDate,
			@UpdatedUserID
			)
			 SET @ExpressFeeTplCity=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@HistoryID", DbType.Int64, info.HistoryID);
            db.AddOutParameter(dbc, "@ExpressFeeTplCity", DbType.Int32, sizeof(Int32));
            db.AddInParameter(dbc, "@ExpressFeeTplID", DbType.Int32, info.ExpressFeeTplID);
            db.AddInParameter(dbc, "@CityID", DbType.Int32, info.CityID);
            db.AddInParameter(dbc, "@UpdateDate", DbType.DateTime, info.UpdateDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedUserID", DbType.Int32, info.UpdatedUserID);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
            return (long)db.GetParameterValue(dbc, "HistoryID");

        }


        public void Update(ExpressFeeTplCityHistoryInfo info)
        {
            StringBuilder sbSql = new StringBuilder("UPDATE ExpressFeeTplCityHistory SET ");
            sbSql.AppendLine("[ExpressFeeTplCity] = @ExpressFeeTplCity,");

            sbSql.AppendLine("[ExpressFeeTplID] = @ExpressFeeTplID,");

            sbSql.AppendLine("[CityID] = @CityID,");

            sbSql.AppendLine("[UpdateDate] = @UpdateDate,");

            sbSql.AppendLine("[UpdatedUserID] = @UpdatedUserID");

            sbSql.Append(" where  [HistoryID] = @HistoryID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@HistoryID", DbType.Int64, info.HistoryID);
            db.AddInParameter(dbc, "@ExpressFeeTplCity", DbType.Int32, info.ExpressFeeTplCity);
            db.AddInParameter(dbc, "@ExpressFeeTplID", DbType.Int32, info.ExpressFeeTplID);
            db.AddInParameter(dbc, "@CityID", DbType.Int32, info.CityID);
            db.AddInParameter(dbc, "@UpdateDate", DbType.DateTime, info.UpdateDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedUserID", DbType.Int32, info.UpdatedUserID);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(long historyID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [ExpressFeeTplCityHistory]  ");
            strSql.Append("WHERE [HistoryID]=@HistoryID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@HistoryID", DbType.Int64, historyID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }


        }

        public IList<ExpressFeeTplCityHistoryInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<ExpressFeeTplCityHistoryInfo> list = new List<ExpressFeeTplCityHistoryInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "ExpressFeeTplCityHistory");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    ExpressFeeTplCityHistoryInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }


        public ExpressFeeTplCityHistoryInfo GetByID(long historyID)
        {
            Model.ExpressFeeTplCityHistoryInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [ExpressFeeTplCityHistory] ");
            strSql.Append("WHERE [HistoryID]=@HistoryID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@HistoryID", DbType.Int64, historyID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }


        #endregion

        /// <summary>
        /// 批量插入
        /// </summary>
        public void BatchInsert(IList<ExpressFeeTplCityHistoryInfo> list)
        {
            var connStr = ConfigurationManager.ConnectionStrings[this.ConnectionStringKey].ConnectionString;
            var conn = new SqlConnection(connStr);
            var tableName = "ExpressFeeTplCityHistory";
            SqlConnectionExtension.BulkInsert<ExpressFeeTplCityHistoryInfo>(conn, tableName, list);
        }
    }
    #endregion
}